//
//  DDDownloadFileACK.m
//  MyClouds
//
//  Created by sarah doo on 13-8-11.
//  Copyright (c) 2013年 sarah doo. All rights reserved.
//

#import "DDDownloadFileACK.h"

@implementation DDDownloadFileACK


- (id)initWithData:(NSData *)data {
    [super initWithData:data];
    return self;
}

- (id)initWithUid:(int)uid andType:(int)type andOffset:(long)off andLength:(int)len andData:(NSData *)data{
    self = [super init];
    NSData* uIdData = [NSData dataWithBytes:&uid length:sizeof(uid)];
    NSRange range;
    range.location = HEADER_LENGTH;
    range.length = INT_SIZE;
    [[self entity]replaceBytesInRange:range withBytes:[uIdData bytes]];
    
    NSData* typeData = [NSData dataWithBytes:&type length:INT_SIZE];
    range.location = range.location + range.length;
    range.length = INT_SIZE;
    [[self entity]replaceBytesInRange:range withBytes:[typeData bytes]];
    
    NSData* offData = [NSData dataWithBytes:&off length:LONG_SIZE];
    range.location = range.location + range.length;
    range.length = LONG_SIZE;
    [[self entity]replaceBytesInRange:range withBytes:[offData bytes]];
    
    NSData* lenData = [NSData dataWithBytes:&len length:INT_SIZE];
    range.location = range.location + range.length;
    range.length = INT_SIZE;
    [[self entity]replaceBytesInRange:range withBytes:[lenData bytes]];
    
    range.location = range.location + range.length;
    range.length = [data length];
    [[self entity]replaceBytesInRange:range withBytes:[data bytes]];
    
    [self setCmd:CloudCmd_DownloadFileAck];
    [self setLength:[self.entity length]];
    int crc = [self crcs:entity withLength:[entity length]];
    [self setCrc:crc];
    return self;
}

- (int)getUid{
    int uid = -1;
    NSRange range;
    range.location = HEADER_LENGTH;
    range.length = INT_SIZE;
    NSData* uidData = [[self entity]subdataWithRange:range];
    [uidData getBytes:&uid length:sizeof(uid)];
    return uid;
}

- (int)getType{
    int type=-1;
    NSRange range;
    range.location = HEADER_LENGTH + INT_SIZE;
    range.length = INT_SIZE;
    NSData* typeData = [[self entity] subdataWithRange:range];
    [typeData getBytes:&type length:sizeof(type)];
    return type;
}

- (long)getOffset{
    long offset = -1l;
    NSRange range;
    int location = HEADER_LENGTH + INT_SIZE + INT_SIZE;
    range.location = location;
    range.length = LONG_SIZE;
    NSData* offsetData = [[self entity]subdataWithRange:range];
    [offsetData getBytes:&offset length:8];
//    offset = [self dataToLong:offsetData];
    return offset;
}

- (int)getLength{
    int length = -1;
    NSRange range;
    range.location = HEADER_LENGTH + INT_SIZE + INT_SIZE + LONG_SIZE;
    range.length = INT_SIZE;
    NSData* lengthData = [[self entity]subdataWithRange:range];
    [lengthData getBytes:&length length:sizeof(length)];
    return length;
}



- (NSData*)getData{
    NSRange range;
    range.location = HEADER_LENGTH + INT_SIZE + INT_SIZE + LONG_SIZE + INT_SIZE;
    range.length = [self length] -range.location;
    NSData* data = [[self entity]subdataWithRange:range];
    return data;
}

-(void)dealloc{
    NSLog(@"------------------------------delloc");
    [self.entity release];
    [self.hdrs release];
    [super dealloc];
}

@end
